//
// Created by 14990 on 2021/12/5.
//
#include <stdlib.h>
#include <stdio.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

typedef struct List {
    Node *head;
    int size;
} List;

Node *getNode(int val) {
    Node *node = (Node *) malloc(sizeof(Node));
    node->data = val;
    node->next = NULL;

    return node;
}

List *getList() {
    List *list = (List *) malloc(sizeof(List));
    list->head = getNode(-1);
    list->size = 0;
    return list;
}

void delete(List *list) {
    Node *head = list->head;
    Node *temp;
    while (head != NULL) {
        temp = head->next;
        free(head);
        head = temp;
    }
    free(list);
}

int insert(List *list, int index, int val) {
    if (list->size < index || index < 0) {
        return -1;
    }
    Node *p = list->head;
    for (int i = 0; i < index; ++i) {
        p = p->next;
    }

    Node *t = getNode(val);
    t->next = p->next;
    p->next = t;
    list->size++;
    return 0;
}

int delete_elem(List *list, int index) {
    if (list->size < 1 || list->size <= index || index < 0) {
        return -1;
    }
    Node *p = list->head;
    for (int i = 0; i < index; ++i) {
        p = p->next;
    }
    Node *t = p->next;
    p->next = t->next;
    free(t);
    list->size--;
    return 0;
}

void print(List *list) {
    printf("size = %d, elements = [ ", list->size);
    Node *head = list->head;
    if (head != NULL) {
        for (Node *t = head->next; t != NULL; t = t->next) {
            printf("%d -> ", t->data);
        }
    }
    printf(" ]\n");
}